package com.atguigu.wc.sink;

import com.atguigu.wc.beans.SensorReading;
import com.atguigu.wc.common.Constant;
import com.atguigu.wc.util.ResourceUtils;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer011;

import java.util.Properties;

/**
 * @Author:GaoFei
 * @Description:
 * @Date:Created in 18:11
 * @Modified By:
 */
public class KafkaSink {
    private static Properties prop = ResourceUtils.load("application.properties");
    private static String brokeList = prop.getProperty("kafka.broker.list");
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> readTextFile = env.readTextFile("D:\\1BIGDATA\\spark-streaming\\Flink\\src\\main\\resources\\hello.txt");
        DataStream<String> map = readTextFile
                .map(line -> {
                    String[] split = line.split(",");
                    return new SensorReading(split[0], new Long(split[1]), new Double(split[1])).toString();
                });
        map.addSink(new FlinkKafkaProducer011<>(brokeList, Constant.KAFKA_TOPIC_NAME,new SimpleStringSchema()));
        env.execute();
    }
}
